package com.sth.three;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import com.sth.utils.RabbitMqUtils;

import java.util.Scanner;

/**
 * @Author: sitianhong
 * @Description: 消息在手动应答是不丢失的, 放回队列中重新消费
 * @Since: create in 2021/6/19 0019 17:08
 */
public class Task02 {

    private static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] argv) throws Exception {
        try (Channel channel = RabbitMqUtils.getChannel()) {
            // 设置不公平 分发
            channel.basicQos(1);
            // 声明队列
            boolean durable = true;
            channel.queueDeclare(TASK_QUEUE_NAME, durable, false, false, null);
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入信息");
            while (sc.hasNext()) {
                String message = sc.nextLine();
                // 休息持久化, 保存在硬盘中
                channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes("UTF-8"));
                System.out.println("生产者发出消息" + message);
            }
        }
    }
}
